package com.jiuli_travel.attraction_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuli_travel.attraction_service.mapper.AttractionCommentsMapper;
import com.jiuli_travel.attraction_service.model.cache.attraction_comment.AttractionCommentLiked;
import com.jiuli_travel.attraction_service.model.dto.attraction_comment.AttractionCommentInsertRequest;
import com.jiuli_travel.attraction_service.model.dto.attraction_comment.AttractionCommentQueryRequest;
import com.jiuli_travel.attraction_service.model.entity.AttractionComment;
import com.jiuli_travel.attraction_service.model.vo.AttractionCommentVO;
import com.jiuli_travel.attraction_service.service.IAttractionCommentService;
import com.jiuli_travel.user_service.model.vo.UserVO;
import com.jiuli_travel.user_service.service.IUserService;
import com.jiuli_travel.utils.PageDTO;
import com.jiuli_travel.utils.Result;
import com.jiuli_travel.utils.UserHolder;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiuli
 * @since 2024-04-01
 */
@Service
public class AttractionCommentServiceImpl extends ServiceImpl<AttractionCommentsMapper, AttractionComment> implements IAttractionCommentService {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private AttractionCommentLiked attractionCommentLiked;

    @Autowired
    private IUserService userService;


    /**
     * 分页获取某个博客的评论信息（底层的回复也是分页查询）初始化形式
     */
    @Override
    public PageDTO<AttractionCommentVO> selectInitAttractionCommentsByPage(Long attractionId){
        /**
         * 获取一级评论的信息
         */
        AttractionCommentQueryRequest attractionCommentQueryRequest = new AttractionCommentQueryRequest();
        attractionCommentQueryRequest.setAttractionId(attractionId);
        attractionCommentQueryRequest.setParentId(0L);
        PageDTO<AttractionCommentVO> attractionCommentVOPageDTO = selectCommentsByPage(attractionCommentQueryRequest);
        if (attractionCommentVOPageDTO == null){
            return null;
        }

        /**
         * 获取 son 评论信息 （一条即可）
         */
        attractionCommentQueryRequest.setPage(1);
        attractionCommentQueryRequest.setSize(1);

        attractionCommentVOPageDTO.getRecords()
                .forEach(attractionCommentVO -> {
//                    /**
//                     * 获取， 封装
//                     */
//                    attractionCommentQueryRequest.setParentId(attractionCommentVO.getId());
//                    PageDTO<AttractionCommentVO> sonAttractionCommentVOPageDTO = selectCommentsByPage(attractionCommentQueryRequest);
//                    attractionCommentVO.setSonAttractionCommentVOList(sonAttractionCommentVOPageDTO);
                });

        return attractionCommentVOPageDTO;
    }

    /**
     * 获取评论信息
     */
    @Override
    public PageDTO<AttractionCommentVO> selectCommentsByPage(AttractionCommentQueryRequest attractionCommentQueryRequest){
        //获取用户的id
        Long userId = UserHolder.getUser().getId();
        /**
         * 对评论信息的操作
         */
        //构造分页排序条件
        Page<AttractionComment> mp = attractionCommentQueryRequest.toMp();

        //构造查询条件并查询 TODO 分页条件查询要在条件中查 （坑批）
        lambdaQuery()
                .eq(attractionCommentQueryRequest.getAttractionId() != null, AttractionComment::getAttractionId, attractionCommentQueryRequest.getAttractionId())
                .eq(AttractionComment::getParentId, attractionCommentQueryRequest.getParentId()).page(mp);

        //校验上是否有评论数据
        if(mp.getTotal() == 0){
            return null;
        }

        //转化数据格式
        PageDTO<AttractionCommentVO> attractionCommentVOPageDTO = PageDTO.of(mp, AttractionComment.class, AttractionCommentVO.class);

        /**
         * TODO 获取该评论者 | 被评论者的基础信息
         */
        attractionCommentVOPageDTO.getRecords()
                .forEach(attractionCommentVO -> {
//                    /**
//                     * 1. TODO 获取评论者的信息
//                     */
//                    //1.1 发起请求获取数据
//                    UserVO userVO = getUserByUserId(attractionCommentVO.getUserId());
//                    //1.2 封装用户信息
//                    attractionCommentVO.setUserVO(userVO);
//
//                    /**
//                     * 2. TODO 获取被评论者的信息 （评论者是一级id, 或者被评论者的是一级评论则不用）
//                     * TODO 后面再完整处理
//                     */
//                    if(attractionCommentVO.getAnswerId() == 0){
//                        return;
//                    }
//                    //1.1 发起请求获取数据
//                    String userName = getUserByUserId(attractionCommentVO.getAnswerId()).getUserName();
//                    //1.2 封装用户信息
//                    attractionCommentVO.setAnswerUserName(userName);
//
//                    /**
//                     * 3. TODO 对该评论进行分析，获取该评论对于当前登录用户是否有点赞
//                     */
//                    boolean flag = attractionCommentLiked.isLikedAttractionComment(userId, attractionCommentVO.getId());
//                    attractionCommentVO.setLikedFlag(flag);

                });
        return attractionCommentVOPageDTO;
    }

    //发表个人博客评论
    @Override
    public void publishAttractionComment(AttractionCommentInsertRequest attractionCommentInsertRequest) {
        //获取发表该评论的用户信息
        Long userId = UserHolder.getUser().getId();
        //添加评论信息
        AttractionComment attractionComment = new AttractionComment();
        attractionComment = BeanUtil.copyProperties(attractionCommentInsertRequest, AttractionComment.class);
        attractionComment.setUserId(userId);
        //创建
        save(attractionComment);
    }

    //删除个人博客评论
    @Override
    public void delAttractionComment(Long attractionCommentId) {
        //TODO 一级评论信息需要管理员进行删除
        /**
         * 判断是不是一级评论信息 TODO 制定规则
         */
        //构造删除条件
        LambdaQueryChainWrapper<AttractionComment> removeWrapper = lambdaQuery()
                .eq(AttractionComment::getId, attractionCommentId)
                .ne(AttractionComment::getParentId, 0);

        //删除
        boolean remove = remove(removeWrapper);
    }

    /**
     * 获取用户的信息
     */
    private UserVO getUserByUserId(Long userId) {
        Result result = userService.getUserByUserId(userId);
        assert result != null;
        return BeanUtil.toBean(result.getData(), UserVO.class);
    }
}
